package com.bwie.record.utils;

import org.apache.commons.codec.binary.Base64;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ImageToBase64 {

    public static String convertImageToBase64(String imagePath) throws IOException {
        // 读取图片文件
        Path path = Paths.get(imagePath);
        byte[] imageBytes = Files.readAllBytes(path);

        // 将图片字节数组转换为Base64编码的字符串
        return Base64.encodeBase64String(imageBytes);
    }

    public static void main(String[] args) {
        try {
            // 替换为你的图片路径
            String imagePath = "path/to/your/image.jpg";
            String base64Image = convertImageToBase64(imagePath);
            System.out.println("Base64编码的图片： " + base64Image);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
